<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
<LINK REL=STYLESHEET TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
<META NAME="GENERATOR" CONTENT="Java2HTML Version 1.3.1">
<TITLE>jena.examples.ontology.persistentOntology.PersistentOntology (Java2HTML)</TITLE>
</HEAD>
<BODY><TABLE id="Header" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td colspan="2" width="33%">&nbsp;</td>
<td align="center" colspan="2" width="33%">
<font size="4">PersistentOntology.java</font>
</td>
<td align="right" colspan="2" width="33%">&nbsp;</td>
</tr>
</TABLE>
<pre ID="Classes">
<A NAME="1"></A><FONT ID="FormalComment">/*****************************************************************************
<A NAME="2"></A> * Source code information
<A NAME="3"></A> * -----------------------
<A NAME="4"></A> * Original author    Ian Dickinson, HP Labs Bristol
<A NAME="5"></A> * Author email       ian.dickinson@hp.com
<A NAME="6"></A> * Package            Jena 2
<A NAME="7"></A> * Web                http://sourceforge.net/projects/jena/
<A NAME="8"></A> * Created            25-Jul-2003
<A NAME="9"></A> * Filename           $RCSfile: PersistentOntology.java.html,v $
<A NAME="10"></A> * Revision           $Revision: 1.4 $
<A NAME="11"></A> * Release status     $State: Exp $
<A NAME="12"></A> *
<A NAME="13"></A> * Last modified on   $Date: 2007/01/17 10:44:23 $
<A NAME="14"></A> *               by   $Author: andy_seaborne $
<A NAME="15"></A> *
<A NAME="16"></A> * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
<A NAME="17"></A> * (see footer for full conditions)
<A NAME="18"></A> *****************************************************************************/</FONT>
<A NAME="19"></A>
<A NAME="20"></A><FONT ID="SingleLineComment">// Package
<A NAME="21"></A></FONT><FONT ID="SingleLineComment">///////////////
<A NAME="22"></A></FONT><FONT ID="Package">package</FONT> jena.examples.ontology.persistentOntology;
<A NAME="23"></A>
<A NAME="24"></A>
<A NAME="25"></A><FONT ID="SingleLineComment">// Imports
<A NAME="26"></A></FONT><FONT ID="SingleLineComment">///////////////
<A NAME="27"></A></FONT><FONT ID="Import">import</FONT> java.util.*;
<A NAME="28"></A>
<A NAME="29"></A><FONT ID="Import">import</FONT> com.hp.hpl.jena.db.*;
<A NAME="30"></A><FONT ID="Import">import</FONT> com.hp.hpl.jena.ontology.*;
<A NAME="31"></A><FONT ID="Import">import</FONT> com.hp.hpl.jena.rdf.model.*;
<A NAME="32"></A>
<A NAME="33"></A>
<A NAME="34"></A><FONT ID="FormalComment">/**
<A NAME="35"></A> * &lt;p&gt;
<A NAME="36"></A> * Simple example of using the persistent db layer with ontology models.  Assumes
<A NAME="37"></A> * that a PostgreSQL database called 'jenatest' has been set up, for a user named ijd.
<A NAME="38"></A> * &lt;/p&gt;
<A NAME="39"></A> *
<A NAME="40"></A> * @author Ian Dickinson, HP Labs
<A NAME="41"></A> *         (&lt;a  href="mailto:Ian.Dickinson@hp.com" &gt;email&lt;/a&gt;)
<A NAME="42"></A> * @version CVS $Id: PersistentOntology.java.html,v 1.4 2007/01/17 10:44:23 andy_seaborne Exp $
<A NAME="43"></A> */</FONT>
<A NAME="44"></A><FONT ID="Public">public</FONT> <FONT ID="Class">class</FONT> PersistentOntology {
<A NAME="45"></A>    <FONT ID="SingleLineComment">// Constants
<A NAME="46"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="47"></A></FONT>
<A NAME="48"></A>
<A NAME="49"></A>    <FONT ID="SingleLineComment">// Static variables
<A NAME="50"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="51"></A></FONT>
<A NAME="52"></A>    <FONT ID="SingleLineComment">// Instance variables
<A NAME="53"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="54"></A></FONT>
<A NAME="55"></A>    <FONT ID="SingleLineComment">// Constructors
<A NAME="56"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="57"></A></FONT>
<A NAME="58"></A>    <FONT ID="SingleLineComment">// External signature methods
<A NAME="59"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="60"></A></FONT>
<A NAME="61"></A>    <FONT ID="Public">public</FONT> <FONT ID="Void">void</FONT> loadDB( ModelMaker maker, String source ) {
<A NAME="62"></A>        <FONT ID="SingleLineComment">// use the model maker to get the base model as a persistent model
<A NAME="63"></A></FONT>        <FONT ID="SingleLineComment">// strict=false, so we get an existing model by that name if it exists
<A NAME="64"></A></FONT>        <FONT ID="SingleLineComment">// or create a new one
<A NAME="65"></A></FONT>        Model base = maker.createModel( source, <FONT ID="False">false</FONT> );
<A NAME="66"></A>
<A NAME="67"></A>        <FONT ID="SingleLineComment">// now we plug that base model into an ontology model that also uses
<A NAME="68"></A></FONT>        <FONT ID="SingleLineComment">// the given model maker to create storage for imported models
<A NAME="69"></A></FONT>        OntModel m = ModelFactory.createOntologyModel( getModelSpec( maker ), base );
<A NAME="70"></A>
<A NAME="71"></A>        <FONT ID="SingleLineComment">// now load the source document, which will also load any imports
<A NAME="72"></A></FONT>        m.read( source );
<A NAME="73"></A>    }
<A NAME="74"></A>
<A NAME="75"></A>    <FONT ID="Public">public</FONT> <FONT ID="Void">void</FONT> listClasses( ModelMaker maker, String modelID ) {
<A NAME="76"></A>        <FONT ID="SingleLineComment">// use the model maker to get the base model as a persistent model
<A NAME="77"></A></FONT>        <FONT ID="SingleLineComment">// strict=false, so we get an existing model by that name if it exists
<A NAME="78"></A></FONT>        <FONT ID="SingleLineComment">// or create a new one
<A NAME="79"></A></FONT>        Model base = maker.createModel( modelID, <FONT ID="False">false</FONT> );
<A NAME="80"></A>
<A NAME="81"></A>        <FONT ID="SingleLineComment">// create an ontology model using the persistent model as base
<A NAME="82"></A></FONT>        OntModel m = ModelFactory.createOntologyModel( getModelSpec( maker ), base );
<A NAME="83"></A>
<A NAME="84"></A>        <FONT ID="For">for</FONT> (Iterator i = m.listClasses(); i.hasNext(); ) {
<A NAME="85"></A>            OntClass c = (OntClass) i.next();
<A NAME="86"></A>            System.out.println( <FONT ID="StringLiteral">"Class "</FONT> + c.getURI() );
<A NAME="87"></A>        }
<A NAME="88"></A>    }
<A NAME="89"></A>
<A NAME="90"></A>
<A NAME="91"></A>    <FONT ID="Public">public</FONT> ModelMaker getRDBMaker( String dbURL, String dbUser, String dbPw, String dbType, <FONT ID="Boolean">boolean</FONT> cleanDB ) {
<A NAME="92"></A>        <FONT ID="Try">try</FONT> {
<A NAME="93"></A>            <FONT ID="SingleLineComment">// Create database connection
<A NAME="94"></A></FONT>            IDBConnection conn  = <FONT ID="New">new</FONT> DBConnection( dbURL, dbUser, dbPw, dbType );
<A NAME="95"></A>
<A NAME="96"></A>            <FONT ID="SingleLineComment">// do we need to clean the database?
<A NAME="97"></A></FONT>            <FONT ID="If">if</FONT> (cleanDB) {
<A NAME="98"></A>                conn.cleanDB();
<A NAME="99"></A>            }
<A NAME="100"></A>
<A NAME="101"></A>            <FONT ID="SingleLineComment">// Create a model maker object
<A NAME="102"></A></FONT>            <FONT ID="Return">return</FONT> ModelFactory.createModelRDBMaker( conn );
<A NAME="103"></A>        }
<A NAME="104"></A>        <FONT ID="Catch">catch</FONT> (Exception e) {
<A NAME="105"></A>            e.printStackTrace();
<A NAME="106"></A>            System.exit( <FONT ID="IntegerLiteral">1</FONT> );
<A NAME="107"></A>        }
<A NAME="108"></A>
<A NAME="109"></A>        <FONT ID="Return">return</FONT> <FONT ID="Null">null</FONT>;
<A NAME="110"></A>    }
<A NAME="111"></A>
<A NAME="112"></A>    <FONT ID="Public">public</FONT> OntModelSpec getModelSpec( ModelMaker maker ) {
<A NAME="113"></A>        <FONT ID="SingleLineComment">// create a spec for the new ont model that will use no inference over models
<A NAME="114"></A></FONT>        <FONT ID="SingleLineComment">// made by the given maker (which is where we get the persistent models from)
<A NAME="115"></A></FONT>        OntModelSpec spec = <FONT ID="New">new</FONT> OntModelSpec( OntModelSpec.OWL_MEM );
<A NAME="116"></A>        spec.setImportModelMaker( maker );
<A NAME="117"></A>
<A NAME="118"></A>        <FONT ID="Return">return</FONT> spec;
<A NAME="119"></A>    }
<A NAME="120"></A>
<A NAME="121"></A>
<A NAME="122"></A>    <FONT ID="SingleLineComment">// Internal implementation methods
<A NAME="123"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="124"></A></FONT>
<A NAME="125"></A>    <FONT ID="SingleLineComment">//==============================================================================
<A NAME="126"></A></FONT>    <FONT ID="SingleLineComment">// Inner class definitions
<A NAME="127"></A></FONT>    <FONT ID="SingleLineComment">//==============================================================================
<A NAME="128"></A></FONT>
<A NAME="129"></A>}
<A NAME="130"></A>
<A NAME="131"></A>
<A NAME="132"></A><FONT ID="MultiLineComment">/*
<A NAME="133"></A>    (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
<A NAME="134"></A>    All rights reserved.
<A NAME="135"></A>
<A NAME="136"></A>    Redistribution and use in source and binary forms, with or without
<A NAME="137"></A>    modification, are permitted provided that the following conditions
<A NAME="138"></A>    are met:
<A NAME="139"></A>
<A NAME="140"></A>    1. Redistributions of source code must retain the above copyright
<A NAME="141"></A>       notice, this list of conditions and the following disclaimer.
<A NAME="142"></A>
<A NAME="143"></A>    2. Redistributions in binary form must reproduce the above copyright
<A NAME="144"></A>       notice, this list of conditions and the following disclaimer in the
<A NAME="145"></A>       documentation and/or other materials provided with the distribution.
<A NAME="146"></A>
<A NAME="147"></A>    3. The name of the author may not be used to endorse or promote products
<A NAME="148"></A>       derived from this software without specific prior written permission.
<A NAME="149"></A>
<A NAME="150"></A>    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
<A NAME="151"></A>    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
<A NAME="152"></A>    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
<A NAME="153"></A>    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
<A NAME="154"></A>    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
<A NAME="155"></A>    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
<A NAME="156"></A>    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
<A NAME="157"></A>    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
<A NAME="158"></A>    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
<A NAME="159"></A>    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<A NAME="160"></A>*/</FONT>
<A NAME="161"></A>
<A NAME="162"></A></pre>
</BODY>
</HTML>